package module;

import entity.LocalVideoConfig;
import lombok.extern.slf4j.Slf4j;
import utils.json.InitConfig;
import utils.json.VideoStateUtil;
import utils.push.PushUtil;
import utils.threadpool.ThreadPoolUtil;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Push {
    private static final VideoStateUtil videoStateUtil = VideoStateUtil.getInstance();
    private static final LocalVideoConfig localVideoConfig = videoStateUtil.getLocalVideoConfig();
    private static final ScheduledExecutorService service = ThreadPoolUtil.getInstance().getTimerThreadPool();
    private static final PushUtil pushUtil = PushUtil.getInstance();

    public static void startPush() {
        log.info(InitConfig.PUSH_DELAY + "分钟后开始定时推送完成日志任务。任务执行间隔时间：" + InitConfig.PERIOD + "分钟。");
        service.scheduleAtFixedRate(new PushRunnable(), InitConfig.PUSH_DELAY, InitConfig.PERIOD, TimeUnit.MINUTES);
    }

    private static class PushRunnable implements Runnable {

        @Override
        public void run() {
            VideoStateUtil.saveConfigToFile();
            pushUtil.PushPlus("本地配置文件状态", localVideoConfig.toJSON().toJSONString());
        }
    }
}
